package com.example.demo1.controller;


import com.example.demo1.mapper.CategoryMapper;
import com.example.demo1.pojo.Category;
import com.example.demo1.pojo.Result;

import com.example.demo1.service.CategoryService;

import jakarta.validation.constraints.Pattern;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/category")
public class CategoryController {

    //注入文章分类服务
    @Autowired
    private CategoryService categoryService;

    @PostMapping
    //新增文章分类
    public Result add(@RequestBody @Validated(Category.Add.class)Category category) {
        categoryService.add(category);
        return Result.success();
    }

    //查询所有文章分类
    @GetMapping
    public Result<List<Category>> list(){
        List<Category> cs = categoryService.list();
        return Result.success(cs);
    }

    //根据id查询
    @GetMapping("/detail")
    public Result<Category> detail(Integer id){
        Category c = categoryService.findById(id);
        return Result.success(c);
    }

    //更新文章分类
    @PutMapping
    public Result update(@RequestBody @Validated(Category.Update.class)Category category) {
        categoryService.update(category);
        return Result.success();
    }



}
